package cn.chenhuanming.chuanzhi.config.secure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

/**
 * Created by Administrator on 2017/2/17.
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserServiceDetails userServiceDetails;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/signIn.html","/signUp","/share/**").permitAll()
                    .anyRequest().authenticated()
                .and().formLogin()
                    .loginPage("/signIn.html")
                    .loginProcessingUrl("/login")
                    .failureHandler(new AjaxLoginFailureHandler())
                    .successHandler(new AjaxLoginSuccessHandler())
                .and().logout().logoutUrl("/logout").logoutSuccessUrl("/signIn.html")
                .and().rememberMe().alwaysRemember(true)
                .and().headers().frameOptions().sameOrigin()
                .and().csrf().ignoringAntMatchers("/imageUpload");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/**");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userServiceDetails).passwordEncoder(new StandardPasswordEncoder());
//                .inMemoryAuthentication().withUser("root").password("root").authorities("q");
    }

}
